//
// File:	InputManager.hpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	30/01/2010 at 02:50:27
// Purpose:	
//
#ifndef INPUTMANAGER_HPP
#define INPUTMANAGER_HPP

#include <list>
#include <string>
#include <SFML/System.hpp>
#include "IInputManager.hpp"
#include "Input.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Input Manager Class
//------------------------------------------------------------------
class InputManager : public IInputManager
{
public:
										InputManager();
										~InputManager();

public:
	bool								AddInput(IInput *);
	void								Update();

public:
	template <typename T> void			Bind(T input, const std::string &inputAction)
	{
		std::list<IInput *>::iterator it = m_inputHandlers.begin();
		std::list<IInput *>::iterator ite = m_inputHandlers.end();
		for (; it != ite; ++it)
		{
			if ((*it)->GetType() == typeid(T))
			{
				Input<T> *pInput = dynamic_cast<Input<T> *>(*it);
				if (pInput)
				{
					Trace::debug << "Binding Action: " << inputAction << " Input: " << typeid(T).name() << std::endl;
					pInput->Bind(input, inputAction);
				}
			}
		}
	}

	template <typename T> void			Unbind(T input)
	{
		std::list<IInput *>::iterator it = m_inputHandlers.begin();
		std::list<IInput *>::iterator ite = m_inputHandlers.end();
		for (; it != ite; ++it)
		{
			if ((*it)->GetType() == typeid(T))
			{
				Input<T> *pInput = dynamic_cast<Input<T> *>(*it);
				if (pInput)
				{
					Trace::debug << "Unbinding Input: " << typeid(T).name() << std::endl;
					pInput->Unbind(input);
				}
			}
		}
	}

	bool								HasInput(const std::string &inputAction) const;

private:
	std::list<IInput *>					m_inputHandlers;
};

#endif //!INPUTMANAGER_HPP
